   package com.sport.controller;

   import cn.dev33.satoken.annotation.SaIgnore;
   import cn.dev33.satoken.stp.SaTokenInfo;
   import cn.dev33.satoken.stp.StpUtil;
   import cn.hutool.core.util.StrUtil;
   import cn.hutool.json.JSONUtil;
   import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
   import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
   import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
   import com.github.xiaoymin.knife4j.annotations.ApiSupport;
   import com.sport.entity.FoodType;
   import com.sport.entity.Motion;
   import com.sport.entity.SysUser;
   import com.sport.entity.dto.MiniLogin;
   import com.sport.entity.enums.ErrorMsg;
   import com.sport.entity.properties.MiniProperties;
   import com.sport.model.PageParams;
   import com.sport.model.Result;
   import com.sport.service.FoodTypeService;
   import com.sport.service.MiniHttpService;
   import com.sport.service.MotionService;
   import com.sport.service.SysUserService;
   import io.swagger.v3.oas.annotations.Operation;
   import io.swagger.v3.oas.annotations.Parameter;
   import io.swagger.v3.oas.annotations.tags.Tag;
   import jakarta.annotation.Resource;
   import org.springframework.beans.factory.annotation.Value;
   import org.springframework.web.bind.annotation.*;
   import com.baomidou.mybatisplus.core.toolkit.Wrappers;

   import java.util.HashMap;
   import java.util.List;
   import java.util.Map;

   @RestController
   @Tag(name = "小程序用户登录管理")
   @RequestMapping("/mini")
   @ApiSupport(author = "开心摆烂", order = 8)
   public class MiniController {

       @Resource
       private MiniProperties properties;

       @Resource
       private MiniHttpService httpService;

       @Resource
       private SysUserService userService;

       @Resource
       private FoodTypeService foodTypeService;

       @Resource
       private MotionService motionService;

       @SaIgnore    //这个是设置白名单的，添加了这个就不会被拦截
       @GetMapping("/login/{code}")
       @ApiOperationSupport(order = 1)
       @Operation(summary = "微信小程序登录", parameters = {@Parameter(name = "code", description = "小程序登录code", required = true)})
       public Result<Map<String, String>> login(@PathVariable String code) {
           String login = httpService.login(properties.getAppid(), properties.getSecret(), code);
           MiniLogin miniLogin = JSONUtil.parseObj(login).toBean(MiniLogin.class);
           if (miniLogin.getErrcode() != null) {
               return ErrorMsg.getResult(miniLogin.getErrcode());
           }
           SysUser user = userService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getOpenid, miniLogin.getOpenid()));
           if (user == null) {
               user = new SysUser();
               user.setOpenid(miniLogin.getOpenid());
               userService.save(user);
           }
           StpUtil.login(user.getId(),"mini");
           Map<String, String> map = new HashMap<>();
           SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
           map.put("tokenHead", tokenInfo.tokenName);
           map.put("token", tokenInfo.tokenValue);
           return Result.success(map);

       }
       @SaIgnore
       @GetMapping("/food/list")
       @ApiOperationSupport(order = 2)
       @Operation(summary = "小程序查询分类")
       public Result<List<FoodType>> list() {
           LambdaQueryWrapper<FoodType> lqw = new LambdaQueryWrapper<>();
           lqw.select(FoodType::getId, FoodType::getTitle, FoodType::getIcon);
           return Result.success(foodTypeService.list(lqw));
       }
       @SaIgnore
       @PostMapping("/home/list")
       @ApiOperationSupport(order = 3)
       @Operation(summary = "小程序首页查询")
       public Result<Page<Motion>> homeList(@RequestBody PageParams<Motion> pageParams) {
           Motion params = pageParams.getParams();
           LambdaQueryWrapper<Motion> lqw = new LambdaQueryWrapper<>();
           lqw.like(StrUtil.isNotBlank(params.getName()),Motion::getName,params.getName());
           lqw.eq(params.getType() != null,Motion::getType,params.getType());
           lqw.like(StrUtil.isNotBlank(params.getBeneficialPosition()),Motion::getBeneficialPosition,params.getBeneficialPosition());
           lqw.eq(Motion::getStatus,1);
           lqw.orderByDesc(Motion::getCreateTime);
           return Result.success(motionService.page(pageParams.getPage(), lqw));
       }
       @SaIgnore
       @GetMapping("/detail/{id}")
       @ApiOperationSupport(order = 4)
       @Operation(summary = "小程序详情查询")
       public Result<Motion> detail(@PathVariable Integer id) {
           return Result.success(motionService.getById(id));
       }

       @GetMapping("/logout")
       @Operation(summary = "退出登录")
       public Result<Boolean> loginout() {
           StpUtil.logout();
           return Result.success();
       }
   }
   